Skip to content

Add more tests to blog post #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2017
Merged

Add more tests to blog post #481

merged 1 commit into from
Mar 14, 2017

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Feb 18, 2017

  • Testing new blog post
  • Testing show blog post (admin)
  • Testing add a comment to post
  • Changing some PostFixtures methods to static for reusability
  • Simplify and reordering some tests

@@ -184,9 +183,9 @@ private function getPhrases()
];
}

private function getRandomPostTiles()
public static function getRandomPostTiles()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yceruto, there is a typo in the method's name here. Should be getRandomPostTitles.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Fixed!

]);

/** @var Post $post */
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yceruto, you could use an EntityManager#find method:

 $post = $client->getContainer()->get('doctrine')->find(Post::class, 1);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry your suggestion does not works.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yceruto, Sorry for confusing you! I mixed up the doctrine service and an entity manager. :(

@lboynton lboynton mentioned this pull request Feb 21, 2017
'PHP_AUTH_USER' => 'john_user',
'PHP_AUTH_PW' => 'kitten',
]);

$client->request($httpMethod, $url);
$this->assertSame($statusCode, $client->getResponse()->getStatusCode());
$this->assertTrue($client->getResponse()->isForbidden());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You made it less strict, but I don't think it makes sense in tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, isForbidden() only checks the 403 status code ... so the code it's technically equivalent...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in this case it's technically equivalent.

@@ -70,7 +70,7 @@ public function testAdminBackendHomePage()
$client = $this->getAdminClient();

$crawler = $client->request('GET', '/en/admin/post/');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertTrue($client->getResponse()->isSuccessful());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here, I think we want to know that status code is exactly 200 OK

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... however, here the isSuccessful() method is too liberal:

    public function isSuccessful()
    {
        return $this->statusCode >= 200 && $this->statusCode < 300;
    }

I agree with @bocharsky-bw and I think we should revert this change and go back to Response::HTTP_OK

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 reverting....

@@ -108,7 +107,7 @@ private function getRandomTags($numTags = 0)
return $tags;
}

private function getPostContent()
public static function getPostContent()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 I'd rather suggest to use PHP Traits instead of making private methods public static or do simple inheritance. We should always avoid public methods which we have to care about later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for Trait

$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
$this->assertNull($post);
/** @var Post $post */
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(31);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 31 id should never fail ... but maybe a findByTitle($postTitle) would make the code more robust?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 sure

'PHP_AUTH_USER' => 'john_user',
'PHP_AUTH_PW' => 'kitten',
]);

$client->request($httpMethod, $url);
$this->assertSame($statusCode, $client->getResponse()->getStatusCode());
$this->assertTrue($client->getResponse()->isForbidden());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, isForbidden() only checks the 403 status code ... so the code it's technically equivalent...

@@ -70,7 +70,7 @@ public function testAdminBackendHomePage()
$client = $this->getAdminClient();

$crawler = $client->request('GET', '/en/admin/post/');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertTrue($client->getResponse()->isSuccessful());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... however, here the isSuccessful() method is too liberal:

    public function isSuccessful()
    {
        return $this->statusCode >= 200 && $this->statusCode < 300;
    }

I agree with @bocharsky-bw and I think we should revert this change and go back to Response::HTTP_OK

@yceruto
Copy link
Member Author

yceruto commented Feb 23, 2017

@javiereguiluz @bocharsky-bw done!

  • Now the fixtures and tests using a FixturesTrait for common methods.
  • and all controller test case now extending from ControllerTestCase to use common decriptive methods and strict assertions for response instances.

Status: Need Reviews :)

Copy link
Member Author

@yceruto yceruto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps Javier could help me with a nice description here :)

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

class ControllerTestCase extends WebTestCase
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing description


namespace Tests;

trait FixturesTrait
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing description

@javiereguiluz
Copy link
Member

I'm not that sure about tests/ControllerTestCase.php. For experienced Symfony developers makes sense ... but for newcomers? They probably see that and think ... "mmm, it's pretty complex to create just one simple functional test ... first I need to create this special ControllerTestCase, then I need to create my tests, etc."

I'm not asking to removing this (yet) ... but let's think a bit about it. Thanks!

@yceruto
Copy link
Member Author

yceruto commented Feb 24, 2017

This ControllerTestCase is just an utility class to make tests more descriptive and simple, it's more related to basic POO inheritance and DRY (no Symfony/funtional Tests related).

I think it's good to show to newcomers good practices related to how to simplify their tests too. For newcomers of "Symfony framework" it should be easy to understand I think, anyway a nice class description could clarify possible misinterpretations,

EDIT: but I'm fine with removing this class, if you prefer :) (or part of it)

@yceruto
Copy link
Member Author

yceruto commented Feb 24, 2017

Maybe I could remove the response assertions and leave only the client creation methods, what do you think?

@yceruto
Copy link
Member Author

yceruto commented Mar 14, 2017

Last changes:

  • Moved response assert to the tests methods again
  • Changed controller test inheritance by trait helper
  • Added comments

@yceruto
Copy link
Member Author

yceruto commented Mar 14, 2017

Rebased (#502 conflicts resolved)

@javiereguiluz
Copy link
Member

@yceruto thanks for all the changes and for rebasing, etc. There are a lot of things that I like in this PR, so I'm going to merge it now. However, there are some things that I don't like because it's not a practice promoted by Symfony (although your proposal works perfectly and it's technically correct). So, I'm going to create a PR soon after to revert some changes. Thanks!

@javiereguiluz javiereguiluz merged commit 2700136 into symfony:master Mar 14, 2017
javiereguiluz added a commit that referenced this pull request Mar 14, 2017
This PR was merged into the master branch.

Discussion
----------

Add more tests to blog post

* Testing new blog post
* Testing show blog post (admin)
* Testing add a comment to post
* Changing some `PostFixtures` methods to static for reusability
* Simplify and reordering some tests

Commits
-------

2700136 Add more tests
@yceruto yceruto deleted the tests branch March 14, 2017 15:39
javiereguiluz added a commit that referenced this pull request Mar 15, 2017
This PR was merged into the master branch.

Discussion
----------

Removed ControllerTestTrait

In #481, @yceruto introduced some nice features. I like them all ... but one of them is not compatible with the way Symfony promotes to do things. The `ControllerTestTrait` is a PHP trait with some helper methods to reduce the boilerplate code in tests. Even if the solution is technically correct, we don't use it in the official Symfony docs. This can create confusion for new developers studying this app and reading the docs.

I propose to revert this feature in this PR ... and later we can discuss about ways to improve this without using traits. Thanks!

Commits
-------

17f9a10 Removed ControllerTestTrait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants